package cn.itcast.nems.payment.web.controller.c;

import cn.itcast.nems.common.util.IPUtil;
import cn.itcast.nems.payment.constant.PayResult;
import cn.itcast.nems.payment.dto.PaymentDTO;
import cn.itcast.nems.payment.service.PaymentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController("cpaymentController")
@RequestMapping("/c/payment")
@Slf4j
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService paymentService;

    /**
     * 学员支付链接跳转
     */
    @GetMapping("/link/{uniqueCode}")
    public ResponseEntity<String> payLink(@PathVariable String uniqueCode, HttpServletResponse response, HttpServletRequest request) throws IOException {
        PaymentDTO paymentDTO = paymentService.payLink(uniqueCode, IPUtil.getIpAddr(request));
        if (PayResult.NOT_REQUIRED.equals(paymentDTO.getPayResult())) {
            return ResponseEntity.ok(
                    String.format("<div style='font-weight:600; font-size:50px'>所报班级“%s”已完成全部缴费。</div>",
                            paymentDTO.getSignClassName())
            );
        } else {
            response.sendRedirect(paymentDTO.getPayLink());
        }
        return null;
    }
}
